package com.example.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.BaseSpecial;
import com.example.mapper.BaseSpecialMapper;
import com.example.service.interfaces.BaseSpecialInterface;
import com.example.trans.Wp;
import com.example.trans.transBaseAirLane;
import com.example.trans.transBaseSpecial;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BaseSpecialService extends ServiceImpl<BaseSpecialMapper, BaseSpecial> implements BaseSpecialInterface {
    @Autowired
    private BaseSpecialMapper baseSpecialMapper;

    public List<transBaseSpecial> findAll() {
        List<BaseSpecial> baseSpecials = baseSpecialMapper.selectList(null);
        return baseSpecials.stream().map(baseSpecial -> {
            transBaseSpecial transBaseSpecial = new transBaseSpecial();
            BeanUtils.copyProperties(baseSpecial, transBaseSpecial);
            String specialwp = baseSpecial.getWaypointsCoordinates();
            if(specialwp != null && !specialwp.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(specialwp);
                List<Wp> wps = jsonArray.toJavaList(Wp.class);
                transBaseSpecial.setWaypointsCoordinates(wps);
            }
            return transBaseSpecial;
        }).collect(Collectors.toList());
    }

    public Optional<transBaseSpecial> findByID(Integer id) {
        BaseSpecial baseSpecial = baseSpecialMapper.selectById(id);
        if(baseSpecial != null) {
            return Optional.empty();
        }
        transBaseSpecial transBaseSpecial = new transBaseSpecial();
        BeanUtils.copyProperties(baseSpecial, transBaseSpecial);
        String specialwp = baseSpecial.getWaypointsCoordinates();
        if(specialwp != null && !specialwp.isEmpty()) {
            JSONArray jsonArray = JSONArray.parseArray(specialwp);
            List<Wp> wps = jsonArray.toJavaList(Wp.class);
            transBaseSpecial.setWaypointsCoordinates(wps);
        }
        return Optional.of(transBaseSpecial);
    }

    public transBaseSpecial isave(transBaseSpecial transBaseSpecial) {
        BaseSpecial baseSpecial = new BaseSpecial();
        BeanUtils.copyProperties(transBaseSpecial, baseSpecial);
        List<Wp> wps = transBaseSpecial.getWaypointsCoordinates();
        if(wps != null && !wps.isEmpty()) {
            String wpJson = JSONArray.toJSONString(wps);
            baseSpecial.setWaypointsCoordinates(wpJson);
        }
        baseSpecialMapper.insert(baseSpecial);
        transBaseSpecial.setId(baseSpecial.getId());
        return transBaseSpecial;
    }

    public Optional<transBaseSpecial> update(transBaseSpecial user) {
        BaseSpecial baseSpecial = new BaseSpecial();
        BeanUtils.copyProperties(user, baseSpecial);
        List<Wp> waypoints = user.getWaypointsCoordinates();
        if(waypoints != null && !waypoints.isEmpty()) {
            String wpJson = JSONArray.toJSONString(waypoints);
            baseSpecial.setWaypointsCoordinates(wpJson);
        }
        int i = baseSpecialMapper.updateById(baseSpecial);
        if(i == 1) {
            return Optional.of(user);
        }
        else {
            return Optional.empty();
        }
    }

    public boolean delete(Integer id) {
        if(baseSpecialMapper.selectById(id) != null) {
            baseSpecialMapper.deleteById(id);
            return true;
        }
        return false;
    }

}